# -*- coding: utf-8 -*-
# @Time        :2025/2/28 15:42
# @Author      :文刀水寿
# @File        : 6_爬虫_urllib_get请求的urlencode方法.py
"""
 @Description : urlencode应用场景:多个参数的时候
"""

# https://www.baidu.com/s?wd=周杰伦&sex=男

# import urllib.parse
#
# data = {
#     'wd':'周杰伦',
#     'sex':'男',
#     'location':'中国台湾省'
# }
#
# a = urllib.parse.urlencode(data)
# print(a)

import urllib.request
import urllib.parse

base_url = 'https://www.baidu.com/s?'

data = {
    'wd': '周杰伦',
    'sex': '男',
    'location': '中国台湾省'
}

new_data = urllib.parse.urlencode(data)

print(new_data)
url = base_url + new_data

# 请求对象的定制
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0',
    'cookie': 'zhishiTopicRequestTime=1740729025625; BIDUPSID=37E7FD100566DBC3BC65F3F7BDBDC4A9; PSTM=1738571532; BAIDUID=9E3BE1F1582275297787F9B02D5D4F8E:FG=1; BAIDUID_BFESS=9E3BE1F1582275297787F9B02D5D4F8E:FG=1; ZFY=UbRkXsnPsyuHe:ASSOh2DZ0QxGpABix:Ah504KQQefL7I:C; H_PS_PSSID=61027_62080_62130_62128_62168_62176_62185_62186_62181_62194_62201_62133_62325; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; __bid_n=195477afa15859268dab62; channel=bing; ab_sr=1.0.1_ZWJkYjRiNWQ5OWZiZGE1Y2FmMjI5MWIyZjZiN2I0MDRmZmEzZmQ0YzIzYTFkYzJiNGY3NzdjMDQ1ZWU2MmYzYmQ5MDJjN2I0NGUwNDQxNjY5ZTMyZTFjNzAxMTk1M2VlNGI4ZmRmZDA0YzJiMjcxMWRlZDRhYTIyYTI1ZTQ5YjNiYmVhOTY4ZmZmOWVhY2JiNzNmYmZhM2YxNzIwMDE0Zg==; baikeVisitId=a7d69de8-da6b-47b6-b31a-b36bc047595f'
}
request = urllib.request.Request(url=url, headers=headers)

# 模拟向服务器发送请求
response = urllib.request.urlopen(request)

# 获取网页内容
content = response.read().decode('utf8')

# 打印
print(content)
